package com.whmnx.my.ui.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;

import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;

import com.whmnx.base.constants.Constants;
import com.whmnx.base.utils.PhotoUtil;
import com.whmnx.my.R;
import com.whmnx.my.databinding.FragmentEditBinding;
import com.whmnx.my.ui.viewmodel.EidtModel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseActivity;

import static com.whmnx.my.ui.viewmodel.EidtModel.CODE_CAMERA_REQUEST;
import static com.whmnx.my.ui.viewmodel.EidtModel.CODE_GALLERY_REQUEST;
import static com.whmnx.my.ui.viewmodel.EidtModel.CODE_RESULT_REQUEST;
import static com.whmnx.my.ui.viewmodel.EidtModel.OUTPUT_X;
import static com.whmnx.my.ui.viewmodel.EidtModel.OUTPUT_Y;

/**
 * author : huang
 * date   : 2020/9/18 0018 16:04
 * version: 1.0
 * @author Administrator
 */
public class MyEditInfoFragment extends BaseActivity<FragmentEditBinding, EidtModel> {

    private Uri imageUri;
    private Uri cropImageUri;

    public List<File> files = new ArrayList<>();

    private File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/photo.jpg");
    private File fileCropUri = new File(Environment.getExternalStorageDirectory().getPath() + "/crop_photo.jpg");

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.fragment_edit;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.initToolbar(MyEditInfoFragment.this);
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CODE_CAMERA_REQUEST:
                    cropImageUri = Uri.fromFile(fileCropUri);
                    PhotoUtil.cropImageUri(this, imageUri, cropImageUri, 1, 1, OUTPUT_X, OUTPUT_Y, CODE_RESULT_REQUEST);
                    break;
                case CODE_GALLERY_REQUEST:
                    cropImageUri = Uri.fromFile(fileCropUri);
                    Uri newUri = Uri.parse(PhotoUtil.getPath(this, data.getData()));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        newUri = FileProvider.getUriForFile(this, "com.whmnx.webcasts.FileProvider", new File(newUri.getPath()));
                    }
                    PhotoUtil.cropImageUri(this, newUri, cropImageUri, 1, 1, OUTPUT_X, OUTPUT_Y, CODE_RESULT_REQUEST);
                    break;
                case CODE_RESULT_REQUEST:
                    Bitmap bitmap = PhotoUtil.getBitmapFromUri(cropImageUri, this);
                    if (bitmap != null) {
                        binding.cIV.setImageBitmap(bitmap);
                        String url = Constants.UPLOADHEADIMG;
                        files.clear();
                        files.add(fileCropUri);

                    }
                    break;
                default:
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
